package arch

import "unsafe"

// EndianType 大小端类型
type EndianType uint8

const (
	LittleEndian EndianType = iota + 1 // 小端序
	BigEndian                          // 大端序
)

// MachineEndianType 机器字节序
var MachineEndianType = detectEndianType()

// IsLittleEndian 检查当前机器的字节序是否小端序
func IsLittleEndian() bool {
	return MachineEndianType == LittleEndian
}

// IsBigEndian 检查当前机器的字节序是否大端序
func IsBigEndian() bool {
	return MachineEndianType != LittleEndian
}

// detectEndianType 检测机器的字节序
func detectEndianType() EndianType {
	var v uint32 = 1
	// 大端序：00 00 00 01
	// 小端序：01 00 00 00
	p := unsafe.Pointer(&v)
	pb := (*byte)(p)
	if *pb == 1 {
		return LittleEndian
	}
	return BigEndian
}
